/*
 * @Descripttion:
 * @Author: chendingfengmail@163.com
 * @version:
 * @Date: 2021-06-15 07:59:24
 * @LastEditors: lzf 742573123@qq.com
 * @LastEditTime: 2023-04-27 20:18:10
 */
import api from '@/api'
import Vue from 'vue'
import { Enum } from "@/commons/enum/enums";

export default {
  namespaced: true,
  state: {
    // 字典
    dict: []
  },
  actions: {

    /**
     * 加载字典列表
     * @param {*} isLoadInCache 是否从缓存加载
     */
     loadDict({ commit, dispatch }, isLoadInCache) {
      if (isLoadInCache) {
        dispatch('load');
      } else {
        dispatch('getDictList');
      }
    },

    // 获取字典列表
    getDictList({ commit, dispatch }) {
      let params = {
        pageIndex: 1,
        pageSize: 99999999,
      }
      return new Promise((resolve, reject) => {
        api.dictInfo.list(params).then((res) => {
          dispatch('set', res.value);
          resolve(res.value);
        }).catch((error) => {
          reject(error)
        })
      })
    },

    /**
     * @description 设置字典数据
     * @param {Object} context
     * @param {*} dict dict
     */
    async set({ state, dispatch }, dict) {
      // store 赋值
      state.dict = dict
      // 持久化
      await dispatch('ddAdmin/db/set', {
        dbName: 'admin',
        path: 'dict',
        value: dict,
        user: true
      }, { root: true })
      // 挂载
      await dispatch('loadDictToVuePrototype', state.dict);
    },
    /**
     * @description 从数据库取字典
     * @param {Object} context
     */
    async load({ state, dispatch }) {
      // store 赋值
      state.dict = await dispatch('ddAdmin/db/get', {
        dbName: 'admin',
        path: 'dict',
        defaultValue: {},
        user: true
      }, { root: true })
      if (state.dict.length > 0) {
        // 挂载
        await dispatch('loadDictToVuePrototype', state.dict);
      }
    },
    /**
     * @description 挂载dict到vue原型上
     * @param {Object} context
     */
    async loadDictToVuePrototype({ state, dispatch }, dict) {
      // 挂载
      Vue.prototype.$dictList = dict;
      await dispatch('loadDictAddToVuePrototypeEnums', dict);
      await dispatch('loadDictAddToVuePrototypeDistEnums', dict);
    },
    /**
     * @description dict追加到Vue.prototype.$enums上
     * @param {Object} context
     */
    async loadDictAddToVuePrototypeEnums({ state, dispatch }, dict) {
      // 挂载
      Vue.prototype.$dictList = dict;
      let dictInfoEnum = new Enum();
      for (let item of dict) {
        // 字典
        dictInfoEnum.add(item.code, item.name, item.id, "", item)
        if (item.dictFieldss && Array.isArray(item.dictFieldss) && item.dictFieldss.length > 0) {
          // 字典属性
          let dictFieldsEnum = new Enum();
          for (let dictField of item.dictFieldss) {
            // 默认0字符串，1数字=转成number
            if (dictField.valueType == 1) { 
              dictField.value1 = (!isNaN(parseFloat(dictField.value1)) && isFinite(dictField.value1)) ? parseInt(dictField.value1) : dictField.value1
            }
            dictFieldsEnum.add(dictField.key, dictField.description, dictField.value1, "", dictField)
          }
          Vue.prototype.$enums[item.code] = dictFieldsEnum
        }
      }
      // 大枚举：Vue.prototype.$enums["DICT"] = dictInfoEnum
      // console.log("vuevue>>>>>>>>>>>>>>>>>>.", Vue.prototype.$dictList);
      // console.log("vuevue>>>>>>>>>>>>>>>>>>2222", Vue.prototype.$enums);

    },

    /**
     * @description dict追加到Vue.prototype.$distEnums上
     * @param {Object} context
     */
    async loadDictAddToVuePrototypeDistEnums({ state, dispatch }, dict){
      let dictInfo = {};
      addDistEnums(dict,dictInfo);
      Vue.prototype.$distEnums = dictInfo;

      function addDistEnums(itemList,dictInfo){
        for(let item of itemList){
          dictInfo[item.code] = {
            field: item.code,
            label: item.name,
            value: item.id,
            dataType: 2 //树类型字典
          }
          if (item.dictFieldss && Array.isArray(item.dictFieldss) && item.dictFieldss.length > 0) {
            for (let dictField of item.dictFieldss) {
              dictInfo[item.code][dictField.key] = {
                field: dictField.key,
                label: dictField.description,
                value: dictField.valueType == 1 && (!isNaN(parseFloat(dictField.value1)) && isFinite(dictField.value1)) ? parseInt(dictField.value1) : dictField.value1,
                dataType: 1 //集合类型字典
              }
            }
          }

          if(item.childrens && Array.isArray(item.childrens) && item.childrens.length > 0){
            addDistEnums(item.childrens,dictInfo[item.code]);
          }
        }
      }
    }
  }
}
